package com.cwc.dao;

import com.cwc.bean.Students;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class StudentsDaoImpl implements StudentsDao{
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public int insertStudents(String number, String name, String gender, String clbum, String grade) {
        String sql = "insert into students(number,name,gender,clbum,grade) values(?,?,?,?,?)";
        int i = jdbcTemplate.update(sql,number,name,gender,clbum,grade);

        return i;
    }

    @Override
    public int updateStudents( String newnumber, String newgender, String newclbum, String newgrade,String name) {
        String sql = "update students set number=?,gender=?,clbum=?,grade=? where name=?";
        int i = jdbcTemplate.update(sql,newnumber,newgender,newclbum,newgrade,name);
        if(i>0){
            System.out.println("修改成功");
        }

        return i;
    }

    @Override
    public List<Students> selectAll() {
        String sql = "select * from students";
        List<Students> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Students>(Students.class));


        return query;
    }

    @Override
    public Students getStudents(String number) {
        String sql = "select * from students where number=?";
        Students students = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Students>(Students.class), number);
        if(students!=null){
            return students;
        }

        return students;
    }

    @Override
    public int update(Students students) {
        String sql = "update students set number=?,gender=?,clbum=?,grade=? where name=?";
        int i = jdbcTemplate.update(sql, students.getNumber(), students.getGender(), students.getClbum(), students.getGrade() , students.getName());
        return i;
    }
}
